
(use-package org
  :bind
  (("C-c l" . org-store-link)
   ("C-c a" . org-agenda))
  :hook
  (org-mode . (lambda () (org-indent-mode 1)))
  :config
  (setq org-ellipsis " ▶"
        org-hide-emphasis-markers t
        org-directory "/Users/binbowang/OneDrive/org"
        org-agenda-files '("gtd/tasks.org"
                           "gtd/journal.org"
                           "gtd/gtd.org"
                           "gtd/PARA.org"
                           )
        org-log-done 'time
        org-agenda-start-with-log-mode t
        org-log-into-drawer t
        org-file-apps '(("\\.docx\\'" . default)
                        ("\\.pdf\\'" . default)
                        ("\\.xlsx\\'" . default)
                        (auto-mode . emacs))
        org-todo-keywords '((sequence "TODO(t)" "NEXT(n)" "TODAY" "|" "DONE(d!)")
                            (sequence "BACKLOG(b)" "PLAN(p)" "READY(r)" "ACTIVE(a)" "REVIEW(v)" "WAIT(w@/!)" "HOLD(h)" "|" "COMPLETED(c)" "CANC(k@)"))))

;; (use-package emacsql-sqlite-builtin)

;; (use-package org-roam
;;   :ensure t
;;   :custom
;;   (setq org-roam-database-connector 'emacsql-sqlite-builtin)
;;   (org-roam-directory (file-truename "/Users/ksas/OneDrive/org/roam-test"))
;;   :bind (("C-c n l" . org-roam-buffer-toggle)
;;          ("C-c n f" . org-roam-node-find)
;;          ("C-c n g" . org-roam-graph)
;;          ("C-c n i" . org-roam-node-insert)
;;          ("C-c n c" . org-roam-capture)
;;          ;; Dailies
;;          ("C-c n j" . org-roam-dailies-capture-today))
;;   ;; :custom
;;   ;; (org-roam-database-connector 'sqlite-builtin)
;;   :config
;;   ;; If you're using a vertical completion framework, you might want a more informative completion interface
;;   (setq org-roam-node-display-template (concat "${title:*} " (propertize "${tags:10}" 'face 'org-tag)))
;;   (org-roam-db-autosync-mode)
;;   ;; If using org-roam-protocol
;;   (require 'org-roam-protocol))

(setq org-refile-targets '((org-agenda-files :maxlevel . 3)
                           ("gtd/gtd.org" :maxlevel . 3)))

(defun dw/read-file-as-string (path)
  (with-temp-buffer
    (insert-file-contents path)
    (buffer-string)))

(setq org-capture-templates
  `(("t" "Tasks / Projects")
    ("tt" "Task" entry (file+olp "./gtd/tasks.org" "Inbox" "研发任务")
     "* TODO %?\n  %U\n  %a\n " :empty-lines 1)
    ("ts" "Task" entry (file+olp "./gtd/tasks.org" "Inbox" "学习任务")
     "* TODO %?\n  %U\n  %a\n" :empty-lines 1)
    ("tr" "Task" entry (file+olp "./gtd/tasks.org" "Inbox" "探索任务")
     "* TODO %?\n  %U\n  %a\n" :empty-lines 1)
    ("tp" "Task" entry (file+olp "./gtd/tasks.org" "Inbox" "个人事务")
     "* TODO %? \n  %U\n  %a\n" :empty-lines 1)
    ("to" "Task" entry (file+olp "./gtd/tasks.org" "Inbox" "管理&杂项")
     "* TODO %?\n  %U\n  %a\n" :empty-lines 1)
    ;; ("ts" "Clocked Entry Subtask" entry (clock)
    ;;  "* TODO %?\n  %U\n  %a\n  %i" :empty-lines 1)

    ("j" "Journal Entries")
    ("jj" "Journal" entry (file+olp+datetree "./gtd/journal.org")
     "* %<%H:%M> - Journal :journal:\n\n%?\n\n"
     :clock-in :clock-resume
     :empty-lines 1)

    ("jm" "Meeting" entry (file+olp+datetree "./gtd/Journal.org")
     "* %<%H:%M> - Meeting :meeting:\n\n%?\n\n"
     :clock-in :clock-resume
     :empty-lines 1)

    ;; ("w" "Workflows")
    ;; ("we" "Checking Email" entry (file+olp+datetree ,(dw/get-todays-journal-file-name))
    ;;      "* Checking Email :email:\n\n%?" :clock-in :clock-resume :empty-lines 1)

    ;; ("m" "Metrics Capture")
    ;; ("mw" "Weight" table-line (file+headline "~/Projects/Code/emacs-from-scratch/OrgFiles/Metrics.org" "Weight")
    ;;  "| %U | %^{Weight} | %^{Notes} |" :kill-buffer t)
  ))


(define-key global-map (kbd "C-c j j")
            (lambda () (interactive) (org-capture nil "jj")))
(define-key global-map (kbd "C-c j m")
            (lambda () (interactive) (org-capture nil "jm")))
(define-key global-map (kbd "C-c t t")
            (lambda () (interactive) (org-capture nil "tt")))
(define-key global-map (kbd "C-c t s")
            (lambda () (interactive) (org-capture nil "ts")))
(define-key global-map (kbd "C-c t r")
            (lambda () (interactive) (org-capture nil "tr")))
(define-key global-map (kbd "C-c t p")
            (lambda () (interactive) (org-capture nil "tp")))
(define-key global-map (kbd "C-c t o")
            (lambda () (interactive) (org-capture nil "to")))


(use-package org-bullets
  :init
  (add-hook 'org-mode-hook (lambda () (org-bullets-mode 1)))
  :custom
  (org-bullets-bullet-list '(;;; Large
                             "◉"
                             "○"
                             "●"
                             "✿"
                             ;; ♥ ● ◇ ✚ ✜ ☯ ◆ ♠ ♣ ♦ ☢ ❀ ◆ ◖ ▶
                             ;;; Small
                             ;; ► • ★ ▸
                             )))

;; Make sure org-indent face is available
(require 'org-indent)

;; Ensure that anything that should be fixed-pitch in Org files appears that way
(set-face-attribute 'org-block nil :foreground nil :inherit 'fixed-pitch)
(set-face-attribute 'org-code nil   :inherit '(shadow fixed-pitch))
(set-face-attribute 'org-indent nil :inherit '(org-hide fixed-pitch))
(set-face-attribute 'org-verbatim nil :inherit '(shadow fixed-pitch))
(set-face-attribute 'org-special-keyword nil :inherit '(font-lock-comment-face fixed-pitch))
(set-face-attribute 'org-meta-line nil :inherit '(font-lock-comment-face fixed-pitch))
(set-face-attribute 'org-checkbox nil :inherit 'fixed-pitch)


;; (require 'org-habit)
;; (add-to-list 'org-modules 'org-habit)
;; (setq org-habit-graph-column 60)


;; (let*
;;     ((variable-tuple (cond ((x-list-fonts "Source Sans Pro") '(:font "Source Sans Pro"))
;;                              ((x-list-fonts "Lucida Grande")   '(:font "Lucida Grande"))
;;                              ((x-list-fonts "Verdana")         '(:font "Verdana"))
;;                              ((x-family-fonts "Sans Serif")    '(:family "Sans Serif"))
;;                              (nil (warn "Cannot find a Sans Serif Font.  Install Source Sans Pro."))))
;;        (base-font-color     (face-foreground 'default nil 'default))
;;        (headline           `(:inherit default :weight bold :foreground ,base-font-color)))

;;   (custom-theme-set-faces 'user
;;                           `(org-level-8 ((t (,@headline ,@variable-tuple))))
;;                           `(org-level-7 ((t (,@headline ,@variable-tuple))))
;;                           `(org-level-6 ((t (,@headline ,@variable-tuple))))
;;                           `(org-level-5 ((t (,@headline ,@variable-tuple))))
;;                           `(org-level-4 ((t (,@headline ,@variable-tuple :height 1.1))))
;;                           `(org-level-3 ((t (,@headline ,@variable-tuple :height 1.25))))
;;                           `(org-level-2 ((t (,@headline ,@variable-tuple :height 1.5))))
;;                           `(org-level-1 ((t (,@headline ,@variable-tuple :height 1.75))))
;;                           `(org-document-title ((t (,@headline ,@variable-tuple :height 1.5 :underline nil)))))
;;   )

(use-package org-download)

;; org past image from clipboard
;; (use-package org-download)
;; (setq org-download-method (quote attach))
;; (setq org-download-screenshot-file "e:/3.jpg")
;; (setq org-download-screenshot-method "magick clipboard: %s")

;; (defun myorg-clipboard (&optional basename)
;;   "Capture the image from the clipboard and insert the resulting file."
;;   (interactive)
;;   (let ((org-download-screenshot-method
;;          (cl-case system-type
;;            ((windows-nt cygwin)
;;             (if (executable-find "magick")
;;                 "magick clipboard: xxxxxxxxxx.jpg"
;;               (user-error
;;                "Please install the \"magick\" program included in ImageMagick")))
;;            )))
;;     (org-download-screenshot basename)))

(setq org-agenda-custom-commands
      '(
        ("u" "Super view"
         ((agenda "" ( (org-agenda-span 1)
                       (org-super-agenda-groups
                        '(
                          (:name "Today"
                                 :tag ("bday" "ann" "hols" "cal" "today")
                                 :time-grid t
                                 :todo ("WIP")
                                 :deadline future
                                 :scheduled today)
                          (:name "Projects"
                                 :category ("Project"))
                          (:name "Areas"
                                 :category ("Area"))
                          (:name "Resources"
                                 :category ("Resource"))
                          (:name "Archives"
                                 :category ("Archive"))
                          (:name "Emacs"
                                 :tag ("emacs" "magit"))
                          (:name "Personal"
                                 :tag ("personal"))
                          (:name "Overdue"
                                 :deadline past)
                          (:name "Reschedule"
                                 :scheduled past)
                          (:name "Due Soon"
                                 :deadline future
                                 :scheduled future)
                          ))))
          (tags (concat "w" (format-time-string "%V"))
                ((org-agenda-overriding-header  (concat "--\nToDos Week " (format-time-string "%V")))
                 (org-super-agenda-groups
                  '((:discard (:deadline t))
                    (:discard (:scheduled t))
                    (:discard (:todo ("DONE")))
                    (:name "Ticklers"
                           :tag "someday")
                    (:name "Personal"
                           :and (:tag "personal" :not (:tag "someday")))
                    (:name "UH"
                           :and (:tag "uh" :not (:tag "someday")))
                    ;; (:name "Neo"
                    ;;  :and (:tag "neo" :not (:tag "someday")))
                    (:name "Ping"
                           :tag "crm")
                    ))))
          ))))


(use-package org-super-agenda
  ;; :straight t
  ;; :after org
  ;; :defer t
  :config
  (org-super-agenda-mode 1)
  (org-agenda nil "u")
  ;; (setq org-super-agenda-groups
  ;;      '(Each group has an implicit boolean OR operator between its selectors.
  ;;        (:name "Today"  Optionally specify section name
  ;;               :time-grid t  Items that appear on the time grid
  ;;               :date today
  ;;               :todo "TODAY"
  ;;               :scheduled today
  ;;               :tag ("today")
  ;;               :order 1)  Items that have this TODO keyword
  ;;        (:name "Important"
  ;;               Single arguments given alone
  ;;               :tag "bills"
  ;;               :priority "A")
  ;;        Set order of multiple groups at once
  ;;        (:order-multi (2 (:name "Shopping in town"
  ;;                                Boolean AND group matches items that match all subgroups
  ;;                                :and (:tag "shopping" :tag "@town"))
  ;;                         (:name "Food-related"
  ;;                                Multiple args given in list with implicit OR
  ;;                                :tag ("food" "dinner"))
  ;;                         (:name "Personal"
  ;;                                :habit t
  ;;                                :tag "personal")
  ;;                         (:name "Space-related (non-moon-or-planet-related)"
  ;;                                Regexps match case-insensitively on the entire entry
  ;;                                :and (:regexp ("space" "NASA")
  ;;                                              Boolean NOT also has implicit OR between selectors
  ;;                                              :not (:regexp "moon" :tag "planet")))))
  ;;        Groups supply their own section names when none are given
  ;;        (:todo "WAITING" :order 8)  Set order of this section
  ;;        (:todo ("SOMEDAY" "TO-READ" "CHECK" "TO-WATCH" "WATCHING")
  ;;               Show this group at the end of the agenda (since it has the
  ;;               highest number). If you specified this group last, items
  ;;               with these todo keywords that e.g. have priority A would be
  ;;               displayed in that group instead, because items are grouped
  ;;               out in the order the groups are listed.
  ;;               :order 9)
  ;;        (:priority<= "B"
  ;;                     Show this section after "Today" and "Important", because
  ;;                     their order is unspecified, defaulting to 0. Sections
  ;;                     are displayed lowest-number-first.
  ;;                     :order 1)
  ;;        After the last group, the agenda will display items that didn't
  ;;        match any of these groups, with the default order position of 99
  ;;        ))
  )

;; general is needed for origami keymaps for org-super-agenda
(use-package general
  ;; :straight t
  )

(use-package origami
  ;; :straight t
  :config
  (defvar ap/org-super-agenda-auto-show-groups
    '("Schedule" "Bills" "Priority A items" "Priority B items"))

  (defun ap/org-super-agenda-origami-fold-default ()
    "Fold certain groups by default in Org Super Agenda buffer."
    (forward-line 3)
    (cl-loop do (origami-forward-toggle-node (current-buffer) (point))
             while (origami-forward-fold-same-level (current-buffer) (point)))
    (--each ap/org-super-agenda-auto-show-groups
      (goto-char (point-min))
      (when (re-search-forward (rx-to-string `(seq bol " " ,it)) nil t)
        (origami-show-node (current-buffer) (point)))))
  :hook ((org-agenda-mode . origami-mode)
         ;;(org-agenda-finalize . ap/org-super-agenda-origami-fold-default)
         )
  :general
  ;; 折叠super-agenda-head
  (:keymaps 'org-super-agenda-header-map
            "TAB" #'origami-toggle-node
            "<tab>" #'origami-toggle-node))


;; blog
(setq org-publish-project-alist
      '(("org-ianbarton"
         ;; Path to org files.
         :base-directory "/Users/ksas/develop/myblog-develop/myblog/org/posts/"
         :base-extension "org"

         ;; Path to Jekyll Posts
         :publishing-directory "/Users/ksas/develop/myblog-develop/myblog/my-awesome-site/_posts/"
         :recursive t
         :publishing-function org-html-publish-to-html
         :headline-levels 4
         :html-extension "html"
         :body-only t
         )
        ("org-static-ian"
         :base-directory "/Users/ksas/develop/myblog-develop/myblog/org/"
         :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|php"
         :publishing-directory "/Users/ksas/develop/myblog-develop/myblog/my-awesome-site/"
         :recursive t
         :publishing-function org-publish-attachment)
        ("ian" :components ("org-ianbarton" "org-static-ian"))
        ))

;; (setq org-publish-project-alist
;;       '(

;;         ("org-ianbarton"
;;          ;; Path to your org files.
;;          :base-directory "/Users/ksas/OneDrive/org/blog/org/_posts/"
;;          :base-extension "org"

;;          ;; Path to your Jekyll project.
;;          :publishing-directory "/Users/ksas/OneDrive/org/blog/jekyll/_posts/"
;;          :recursive t
;;          :publishing-function org-publish-org-to-html
;;          :headline-levels 4
;;          :html-extension "html"
;;          :body-only t ;; Only export section between <body> </body>
;;          )


;;         ("org-static-ian"
;;          :base-directory "/Users/ksas/OneDrive/org/blog/org/"
;;          :base-extension "css\\|js\\|png\\|jpg\\|gif\\|pdf\\|mp3\\|ogg\\|swf\\|php"
;;          :publishing-directory "~/devel/ianbarton/"
;;          :recursive t
;;          :publishing-function org-publish-attachment)

;;         ("ian" :components ("org-ianbarton" "org-static-ian"))

;;         ))

;; (use-package org-roam
;;   :ensure t
;;   :custom
;;   (org-roam-directory (file-truename "/path/to/org-files/"))
;;   :bind (("C-c n l" . org-roam-buffer-toggle)
;;          ("C-c n f" . org-roam-node-find)
;;          ("C-c n g" . org-roam-graph)
;;          ("C-c n i" . org-roam-node-insert)
;;          ("C-c n c" . org-roam-capture)
;;          ;; Dailies
;;          ("C-c n j" . org-roam-dailies-capture-today))
;;   :config
;;   ;; If you're using a vertical completion framework, you might want a more informative completion interface
;;   (setq org-roam-node-display-template (concat "${title:*} " (propertize "${tags:10}" 'face 'org-tag)))
;;   (org-roam-db-autosync-mode)
;;   ;; If using org-roam-protocol
;;   (require 'org-roam-protocol))

(provide 'setup-org)
